package com.liuche.rpc.provider;

/**
 * @author：liuche
 * @Date：2024年03月07日 15:40
 * @Description：
 */

import com.liuche.rpc.RpcApplication;
import com.liuche.rpc.common.service.UserService;
import com.liuche.rpc.config.RegistryConfig;
import com.liuche.rpc.config.RpcConfig;
import com.liuche.rpc.model.ServiceMetaInfo;
import com.liuche.rpc.registry.LocalRegistry;
import com.liuche.rpc.registry.Registry;
import com.liuche.rpc.registry.RegistryFactory;
import com.liuche.rpc.server.HttpServer;
import com.liuche.rpc.server.VertxHttpServer;

/**
 * 简易服务消费者示例
 */
public class ProviderExample {

    public static void main(String[] args) {
        // RPC 框架初始化
        RpcApplication.init();

        // 注册服务

        // 得到系统配置参数
        RpcConfig rpcConfig = RpcApplication.getRpcConfig();
        RegistryConfig registryConfig = rpcConfig.getRegistryConfig();

        // 得到注册中心
        Registry registry = RegistryFactory.getInstance(registryConfig.getRegistry());
        // 封装参数
        ServiceMetaInfo serviceMetaInfo = new ServiceMetaInfo();
        serviceMetaInfo.setServiceName(rpcConfig.getName());
        serviceMetaInfo.setServiceVersion("1.0");
        serviceMetaInfo.setServiceHost(rpcConfig.getServerHost());
        serviceMetaInfo.setServicePort(rpcConfig.getServerPort().toString());
        serviceMetaInfo.setServiceAddress(rpcConfig.getServerHost() + ":" + rpcConfig.getServerPort());
        try {
            registry.register(serviceMetaInfo);
        }catch (Exception e) {
            System.out.println(e);
        }




        // 注册服务
        LocalRegistry.register(UserService.class.getName(), UserServiceImpl.class);

        // 启动 web 服务
        HttpServer httpServer = new VertxHttpServer();
        httpServer.doStart(rpcConfig.getServerPort());
    }
}
